<?php

namespace app\models;

use Yii;
use yii\db\ActiveRecord;
use yii\behaviors\TimestampBehavior;
use app\models\events\UploadEvent;
use app\models\behaviors\SortBehavior;
/**
 * This is the model class for table "user".
 *
 * @property integer $userid
 * @property string $name
 * @property string $img
 * @property integer $sex
 * @property string $tele
 * @property string $pwd
 * @property double $take_money
 * @property double $give_money
 * @property string $province
 * @property string $city
 * @property string $county
 * @property integer $type
 * @property integer $phone_type
 * @property integer $fid
 * @property string $sort
 * @property integer $created_at
 * @property integer $updated_at
 */
class User extends ActiveRecord
{
    const PHONE_TYPE_LEAVE = 0;
    const PHONE_TYPE_IOS = 1;
    const PHONE_TYPE_ANDROID = 2;

    const SCENARIO_CREATE = 'create';//创建用户
    const SCENARIO_CREATE_ROBOT = 'create-robot';//创建机器人
    const SCENARIO_UPDATE_ROBOT = 'update-robot';//修改机器人
    const SCENARIO_REGISTER = 'register';//注册
    const SCENARIO_PHONE_CHANGE = 'phone-change';//修改

    const IS_ROBOT = 1;//机器人
    const IS_USER = 0;//用户

    const IS_MAN = 0;//男
    const IS_WOMAN = 1;//女

    public static function dropdown($column, $value = null)
    {
        $dropDownList = [
            'phone_type'=>[
                self::PHONE_TYPE_LEAVE=>'离线',
                self::PHONE_TYPE_IOS=>'iOS',
                self::PHONE_TYPE_ANDROID=>'安卓',
            ],
            'type'=> [
                self::IS_ROBOT=>'机器人',
                self::IS_USER=>'用户',
            ],
            'sex'=> [
                self::IS_MAN=>'男',
                self::IS_WOMAN=>'女',
            ],
        ];
        if ($value !== null)
            return array_key_exists($column, $dropDownList) ? $dropDownList[$column][$value] : false;
        else
            return array_key_exists($column, $dropDownList) ? $dropDownList[$column] : false;
    }
    public static function getUsers()
    {
        return self::find()->where(['type'=>self::IS_USER])->all();
    }
    public function behaviors()
    {
        return [
            [
                'class' => TimestampBehavior::className(),
                'attributes'=>[
                    ActiveRecord::EVENT_BEFORE_INSERT => ['created_at', 'updated_at'],
                    ActiveRecord::EVENT_BEFORE_UPDATE => ['updated_at'],
                ],
            ],
            [
                'class'=>UploadEvent::className(),
                'changeAttribute'=>'img',
            ],
            [
                'class'=>SortBehavior::className(),
                'idattribute'=>'userid',
            ]
        ];
    }
    /**
     * @inheritdoc
     */
    public static function tableName()
    {
        return 'user';
    }

    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['name', 'img', 'tele', 'pwd', 'province', 'city', 'county'], 'required'],
            ['name','unique'],
            ['tele','unique'],
            [['sex', 'type'], 'integer'],
            ['type','default','value'=>function ($model, $attribute) {
                return self::getScenario() == self::SCENARIO_CREATE_ROBOT?self::IS_ROBOT:self::IS_USER;
            }],
            [['sex','take_money','fid'],'default','value'=>0],
            ['give_money','default','value'=>5],
            ['phone_type','default','value'=>self::PHONE_TYPE_LEAVE],
            [['name', 'img', 'tele', 'pwd'], 'string', 'max' => 255],
            ['tele','match','pattern'=>'/((\d{11})|^((\d{7,8})|(\d{4}|\d{3})-(\d{7,8})|(\d{4}|\d{3})-(\d{7,8})-(\d{4}|\d{3}|\d{2}|\d{1})|(\d{7,8})-(\d{4}|\d{3}|\d{2}|\d{1}))$)/','message'=>'不能识别的电话'],
            [['province', 'city', 'county'], 'string', 'max' => 200],
        ];
    }

    /**
     * @inheritdoc
     */
    public function attributeLabels()
    {
        return [
            'userid' => 'ID',
            'name' =>'昵称',
            'img' =>'头像',
            'sex' => '性别',
            'tele' =>'电话',
            'pwd' => '密码',
            'take_money' => '拍币',
            'give_money' => '赠币',
            'province' => '省',
            'city' => '市',
            'county' => '区',
            'type' => '类型',
            'fid'=>'上级会员',
            'created_at' => '创建时间',
            'updated_at' => '修改时间',
        ];
    }

    /**
     * 验证场景
     * @return array
     */
    public function scenarios()
    {
        $scenarios = parent::scenarios();
        $scenarios[self::SCENARIO_CREATE] = ['name','img','province','city','county','type','tele','pwd','sex','fid'];
        $scenarios[self::SCENARIO_CREATE_ROBOT] = ['name','img','province','city','county','type','sex'];
        $scenarios[self::SCENARIO_UPDATE_ROBOT] = ['name','img','province','city','county','type','sex'];
        $scenarios[self::SCENARIO_REGISTER] = ['name','province','city','county','type','tele','pwd','phone_type','sex','take_money','give_money','fid'];
        $scenarios[self::SCENARIO_PHONE_CHANGE] = ['img','name','tele'];
        return $scenarios;
    }


    public function validatePassword($password)
    {
        return $this->pwd === $password;
    }

    public function getCollects()
    {
        return $this->hasMany(Product::className(),['pid'=>'product_id'])
            ->viaTable('collect',['user_id'=>'userid']);
    }

    /**
     * 减法
     * @param $money
     * @return array
     */
    public function reduceMoney($money){
        $data = [];
        if($this->give_money>$money){
            $use_give_money = $money;
            $use_take_money = 0;
            $this->give_money-=$money;
        }else{
            $use_give_money = $this->give_money;
            $use_take_money = $money-$this->give_money;
            $this->give_money -= $use_give_money;
            $this->take_money -= $use_take_money;
        }
        $data['give_money'] = $use_give_money;
        $data['take_money'] = $use_take_money;
        return $data;
    }
}
